user_base = {'root': {'password': '123456'},'liuxing': {'password': '1221'}}
shop = {'F00001': {'name': '苹果', 'price': 1.2},'F00002': {'name': '香蕉', 'price': 5.5}}
shopping_car = {}
sum_price = 0
sum_qty = 0
def enter():
    print("欢迎来到三乐购物系统".center(50,'*'))
    global username,password,user_money
    username = input("请输入您的用户名：")
    password = input("请输入您的密码：")
    if username == exit:
        exit()
    elif username in user_base and password == user_base[username].get('password'):
        print("登录成功")
        user_money = float(input("请输入您的购物预算："))
        print(f"商品信息为{shop}")
        return shopping()
    else:
        print("输入密码错误或用户不存在!\n请重新输入！")
        return enter()
def shopping():
    global sum_price,user_money,shopping_car,sum_qty
    while True:
        goods_num = input("请输入您要购买的商品编号：")
        if goods_num == 'exit':
            exit()
            return
        elif goods_num in shop:
            goods_qty = input("请输入您要购买的数量：")
            if goods_qty.isdigit():
                goods_qty = float(goods_qty)
                goods_name = shop[goods_num].get('name')
                goods_price = shop[goods_num].get('price')
                goods_sum_price = goods_qty * goods_price
                if user_money > goods_sum_price:
                    if goods_name in shopping_car:
                        sum_price += goods_sum_price
                        user_money -= goods_sum_price
                        sum_qty = goods_qty + shopping_car[goods_name]['已购买数量']
                        shopping_car.update(
                            {shop[goods_num]['name']: {'单价': shop[goods_num]['price'], '已购买数量': sum_qty}})
                        print(f"购物信息：{shopping_car}")
                        print(f"购物总金额：{sum_price:.2f},您的预算余额{user_money:.2f}")
                    else:
                        sum_price += goods_sum_price
                        user_money -= goods_sum_price
                        shopping_car.update({shop[goods_num]['name']: {'单价': shop[goods_num]['price'],'已购买数量':goods_qty}})
                        print(f"购物信息：{shopping_car}")
                        print(f"购物总金额：{sum_price:.2f},您的预算余额{user_money:.2f}")
                else:
                    print(f"余额不足，您的预算余额还剩{user_money:.2f},购物总金额为{sum_price:.2f}")
                    next = input("充值金额请输入1\n退出购物请输入2\n")
                    if next == '1':
                        money = float(input("请输入您要充值的金额："))
                        user_money += money
                        return shopping()
                    else:
                        return exit()
            else:
                print("输入有误，请重新输入")
        else:
            print("非常抱歉哦！没有您要购买的商品。")
def exit():
    print(f"您的购物信息为:{shopping_car},购物总金额为：{sum_price:.2f},预算余额为{user_money:.2f}")
enter()

